package com.czx.aspect;

import com.czx.decorator.impl.RedisCacheServiceImpl;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

@Component
@Aspect
public class CustomCacheAspect {

    @Autowired
    private RedisCacheServiceImpl redisCacheService;

    @Around("@annotation(com.czx.anno.CustomCache)")
    public Object aroud(ProceedingJoinPoint proceedingJoinPoint) {
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = methodSignature.getMethod();
        String key = method.getName() + Arrays.toString(method.getParameterTypes()) + Arrays.toString(proceedingJoinPoint.getArgs());
        return redisCacheService.get(key, method.getReturnType(), proceedingJoinPoint);
    }
}
